package cn.com.hongfu.controller;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import cn.com.hongfu.model.CaseImg;
import cn.com.hongfu.model.Rule;
import cn.com.hongfu.service.CaseService;
import cn.com.hongfu.service.KnowledgeService;
import cn.com.hongfu.service.PlayService;
import cn.com.hongfu.service.RuleService;
@Controller
@RequestMapping("/index")
public class IndexController {
	@Autowired
	public RuleService ruleService;
	@Autowired
	public KnowledgeService knowledgeService ;
	@Autowired
	public CaseService caseService ;
	@Autowired
	public PlayService playService ;
	/**
	 * 返回主页视图
	 * @return
	 */
	@RequestMapping("/index")
	public String index(ModelMap modelMap){
		modelMap.addAttribute("ks", knowledgeService.getKnowledgeForIndex());
		modelMap.addAttribute("cs", caseService.getCaseForIndex());
		modelMap.addAttribute("plays", playService.getAll());
		return "/index/index";
	}
	/**
	 * 返回案例视图
	 * @return
	 */
	@RequestMapping("/case")
	public String Case(ModelMap modelMap){
		modelMap.addAttribute("flag", "case");
		List<cn.com.hongfu.model.Case> cs = caseService.getAll();
		List<cn.com.hongfu.model.Case> csN = new ArrayList<cn.com.hongfu.model.Case>();
		for(cn.com.hongfu.model.Case c : cs){
			List<CaseImg> ci = caseService.getImgByCaseId(c.getId());
			if(ci.size() > 0)
				c.setCoverPhoto(ci.get(0).getSrc());
			else
				c.setCoverPhoto("images/anli2.jpg");
			csN.add(c);
		}
		modelMap.addAttribute("cs", csN);
		return "/index/case";
	}
	
	/**
	 * 返回案例视图
	 * @return
	 */
	@RequestMapping("/caseDesc")
	public String caseDesc(ModelMap modelMap,HttpServletRequest request){
		modelMap.addAttribute("flag", "case");
		int id = Integer.parseInt(request.getParameter("id"));
		cn.com.hongfu.model.Case c = caseService.getCaseById(id);
		modelMap.addAttribute("c", c);
		modelMap.addAttribute("cImgs", caseService.getImgByCaseId(id));
		return "/index/caseDesc";
	}
	
	/**
	 * 返回套餐视图
	 * @return
	 */
	@RequestMapping("/meal")
	public String meal(ModelMap modelMap){
		modelMap.addAttribute("flag", "meal");
		return "/index/meal";
	}
	/**
	 * 返回装修常识视图
	 * @return
	 */
	@RequestMapping("/knowledge")
	public String knowledge(ModelMap modelMap){
		modelMap.addAttribute("flag", "knowledge");
		modelMap.addAttribute("ks", knowledgeService.getAllKnowledge());
		return "/index/knowledge";
	}
	
	/**
	 * 返回装修常识详细视图
	 * @return
	 */
	@RequestMapping("/knowledgeDesc")
	public String knowledgeDesc(ModelMap modelMap,HttpServletRequest request){
		modelMap.addAttribute("flag", "knowledge");
		int kId = Integer.parseInt(request.getParameter("kId"));
		modelMap.addAttribute("k", knowledgeService.getKnowledgeById(kId));
		return "/index/knowledgeDesc";
	}
	/**
	 * 返回装修常识视图
	 * @return
	 */
	@RequestMapping("/about")
	public String about(ModelMap modelMap){
		modelMap.addAttribute("flag", "about");
		Rule rule = ruleService.getRuleByName("hongfu");
		modelMap.addAttribute("rule", rule);
		return "/index/about";
	}
	
	/**
	 * 返回我要装修视图
	 * @return
	 */
	@RequestMapping("/require")
	public String require(ModelMap modelMap){
		modelMap.addAttribute("flag", "require");
		return "/index/require";
	}
	/**
	 * 返回导航视图
	 * @return
	 */
	@RequestMapping("/head")
	public String head(ModelMap modelMap){
		return "/index/head";
	}
	/**
	 * 返回底部视图
	 * @return
	 */
	@RequestMapping("/bottom")
	public String bottom(ModelMap modelMap){
		return "/index/bottom";
	}
	/**
	 * 返回QQ客服视图
	 * @return
	 */
	@RequestMapping("/qqService")
	public String qqService(ModelMap modelMap){
		return "/index/qqService";
	}
}
